package org.example.service.Impl;

import lombok.AllArgsConstructor;
import org.example.dao.Like;
import org.example.dao.Song;
import org.example.enums.responseEnum;
import org.example.mapper.SongMapper;
import org.example.service.LikeService;
import org.example.service.SongService;
import org.example.util.MinioUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

@Service
@AllArgsConstructor
public class SongServiceImpl implements SongService {


    private final SongMapper songMapper;

    private final MinioUtil minioUtil;

    private final LikeService likeService;


    //获取歌曲列表
    @Override
    public List<Song> getSongList(Integer uid) {
        //拿到数据库中的所有歌曲
        List<Song> songList = songMapper.getSongList();
        //如果uid不为空才请求
        if (uid != null) {
            //拿到所有的点赞记录
            List<Like> likeList = likeService.getLikeList();
            for (Like like : likeList) {
                if (like.getUid().equals(uid)) {
                    for (Song song : songList) {
                        if (like.getSid().equals(song.getId())) {
                            song.setLike(true);
                        }
                    }
                }
            }
        }
        //调用歌曲工具类方法，直接操纵地址，添加ip
        minioUtil.getSongUrl(songList);
        Collections.shuffle(songList);
        return songList;
    }

    @Override
    public Song getSongById(Integer id) {
        return songMapper.getSongById(id);
    }

    //搜索歌曲
    @Override
    public List<Song> search(String text, Integer uid) {
        List<Song> songList = songMapper.search(text);
        minioUtil.getSongUrl(songList);
        return songList;
    }

    //添加歌曲
    @Override
    public responseEnum addSong(Song song) {
        Integer i = songMapper.addSong(song);
        if (i > 0) {
            return responseEnum.success;
        } else {
            return responseEnum.error;
        }
    }

    @Override
    public responseEnum updateSongCommentCount(Song song) {
        Integer i = songMapper.updateSongComment(song);
        if (i > 0) {
            return responseEnum.success;
        } else {
            return responseEnum.error;
        }
    }

    @Override
    public responseEnum updateSongLikeCount(Song song) {
        Integer i = songMapper.updateSongLikeCount(song);
        if (i > 0) {
            return responseEnum.success;
        } else {
            return responseEnum.error;
        }
    }

    //修改歌曲


    @Override
    public responseEnum addSongIntact(MultipartFile file, Song song) {
        return null;
    }


}
